package com.链表2;

/**
 * 删除链表中等于给定值 val 的所有节点。
 *
 * 示例:
 *
 * 输入: 1->2->6->3->4->5->6, val = 6
 * 输出: 1->2->3->4->5
 */
public class 移除链表元素 {
    static class Solution {
        public ListNode removeElements(ListNode head, int val) {
            ListNode pre = new ListNode(-1);
            pre.next = head;
            ListNode cur = pre;
            while (cur != null && cur.next != null ){
                if(cur.next.val == val){
                    cur.next = cur.next.next;
                }
                else {
                    cur = cur.next;
                }
            }
            return pre.next;
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        MyLinkedList link = new MyLinkedList(new int[]{1,1,1,1,1});
        ListNode node = solution.removeElements(link.head.next,1);
    }
}
